import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
	state:{
		userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : null,
		token: uni.getStorageSync('token') ? uni.getStorageSync('token') : null,
	},
	mutations:{
		MLOGIN(state, userInfo){
			uni.setStorageSync('userInfo', userInfo)
			state.userInfo = userInfo
		},
		MLOGOUT(state){
			//退出逻辑，保留协议不变
			const storageSync = uni.getStorageSync("agreement");
			uni.clearStorageSync()
			uni.setStorageSync("agreement", storageSync)
			state.userInfo = null
		},
		MSETTOKEN(state, token){
			uni.setStorageSync('token', token)
			state.token = token
		},
		MCLEARTOKEN(state){
			uni.removeStorageSync('token')
			state.token = null
		}
	},
	actions:{
		setToken(context, token){
			context.commit('MSETTOKEN', token)
		},
		clearToken(context){
			context.commit('MCLEARTOKEN')
		},
		login(context, userInfo){
			context.commit('MLOGIN', userInfo)
		},
		logout(context){
			context.commit('MLOGOUT')
		}

	},
	getters: {

		//检查是否登录
		checkLogin(state) {
			if(state.userInfo){
				return true;
			}
			return false;
		}
	}
})

export default store
